/********************************************************************************
 * BannerAccountsServiceDelegator.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Sep 10, 2008 4:09:14 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/

package com.printeverywhere.delegator;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import com.printeverywhere.dto.BannerZones;
import com.printeverywhere.util.HibernateUtil;

/**
 * The Class BannerZonesServiceDelegator.
 */
public class BannerZonesServiceDelegator {
	
	/** The log. */
	private static Logger log = Logger.getLogger(BannerZonesServiceDelegator.class);

	/**
	 * Gets the auction listing fees.
	 * 
	 * @return the auction listing fees
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	@SuppressWarnings("unchecked")
	public static List<BannerZones> getBannerZones() throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for BannerZones without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			List<BannerZones> bannerCampaignsList = session.createCriteria(BannerZones.class).list();
			transaction.commit();
			return bannerCampaignsList;
		} catch (HibernateException e) {
			log.error("Got exception when getting list for BannerZones without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Gets the banner zone by id.
	 * 
	 * @param zoneId the zone id
	 * 
	 * @return the banner zone by id
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static BannerZones getBannerZoneById(int zoneId) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for BannerZones without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(BannerZones.class);
			criteria.add(Restrictions.eq("id", zoneId));
			List<BannerZones> listBannerZone = criteria.list();
			transaction.commit();
			return (listBannerZone.size() > 0) ? listBannerZone.get(0) : new BannerZones();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for BannerZones without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Update mailing system.
	 * 
	 * @param BannerZones the mailing system
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void updateBannerZones(BannerZones BannerZones) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called updateBannerZones method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(BannerZones);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when update BannerZones without search criteria".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Delete banner zones.
	 * 
	 * @param bannerZones the banner zones
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void deleteBannerZones(BannerZones bannerZones) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called updateBannerZones method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.delete(bannerZones);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when update BannerZones without search criteria".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Inser banner accounts.
	 * 
	 * @param BannerZones the banner accounts
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void inserBannerZones(BannerZones BannerZones) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called inserBannerZones method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(BannerZones);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when insert BannerZones without search criteria".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Count banner campaigns.
	 * 
	 * @param campaignId the campaign id
	 * 
	 * @return the int
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static int countBannerZones(int campaignId) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for BannerZones without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(BannerZones.class);
			criteria.add(Restrictions.eq("bannerAccount.id", campaignId));
			criteria.setProjection(Projections.rowCount());
			transaction.commit();
			return (Integer)criteria.uniqueResult();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for BannerZones without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}
}